package com.yunti.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunti.springboot.commonenum.SemesterEnum;
import com.yunti.springboot.mapper.ElectiveSubMapper;
import com.yunti.springboot.mapper.OptionMapper;
import com.yunti.springboot.mapper.SubjectMapper;
import com.yunti.springboot.po.*;
import com.yunti.springboot.service.OptionService;
import com.yunti.springboot.service.StudentService;
import com.yunti.springboot.vo.Result;
import com.yunti.springboot.vo.req.GetCouldOptionListReq;
import com.yunti.springboot.vo.req.SearchStudentReq;
import com.yunti.springboot.vo.req.SelectSubReq;
import com.yunti.springboot.vo.res.StudentWithElectiveSubRes;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class OptionServiceImpl extends ServiceImpl<OptionMapper, Option> implements OptionService {

    @Resource
    private SubjectMapper subjectMapper;
    @Resource
    private OptionMapper optionMapper;

    @Resource
    private ElectiveSubMapper electiveSubMapper;

    @Resource
    private StudentService studentService;

    @Override
    public Result getCouldOptionList(GetCouldOptionListReq getAllExamSubByClassReq) {
        SemesterEnum semesterEnum = SemesterEnum.getEnumBySemester(getAllExamSubByClassReq.getSemester());
        LambdaQueryWrapper<Subject> subjectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        subjectLambdaQueryWrapper.eq(Subject::getYear, semesterEnum.getYear());
        subjectLambdaQueryWrapper.eq(Subject::getType, getAllExamSubByClassReq.getType());

        List<Subject> subjects = subjectMapper.selectList(subjectLambdaQueryWrapper);

        return Result.success(subjects);
    }

    @Override
    public Result selectSub(SelectSubReq selectSubReq) {
        Option option = new Option();
        BeanUtils.copyProperties(selectSubReq, option);
        option.setCreateTime(LocalDate.now());

        LambdaQueryWrapper<Option> optionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        optionLambdaQueryWrapper.eq(Option::getStuId, selectSubReq.getStuId());
        optionLambdaQueryWrapper.eq(Option::getSubId, selectSubReq.getSubId());
        if(baseMapper.exists(optionLambdaQueryWrapper)){
            return Result.error("禁止重复选科");
        }
        LambdaQueryWrapper<Option> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Option::getElectiveSubId, selectSubReq.getElectiveSubId());
        lambdaQueryWrapper.eq(Option::getStuId, selectSubReq.getStuId());

        if (baseMapper.exists(lambdaQueryWrapper)) {
            update(option, lambdaQueryWrapper);
        } else {
            save(option);
        }
        return Result.success("选科成功");
    }

    @Override
    public Result del(SelectSubReq selectSubReq) {
        optionMapper.deleteById(selectSubReq.getId());
        return Result.success("删除成功");
    }

    @Override
    public Result getAllOptionByClass(SearchStudentReq searchStudentReq) {
        LambdaQueryWrapper<ElectiveSub> electiveSubLambdaQueryWrapper = new LambdaQueryWrapper<>();
        electiveSubLambdaQueryWrapper.eq(ElectiveSub::getSemester, searchStudentReq.getSemester());
        electiveSubLambdaQueryWrapper.eq(ElectiveSub::getSubId, -1);
        List<ElectiveSub> electiveSubList = electiveSubMapper.selectList(electiveSubLambdaQueryWrapper);
        List<Integer> electiveSubIdList = electiveSubList.stream().map(ElectiveSub::getId).collect(Collectors.toList());

        Result<Page<Student>> result = studentService.getStudentsInfo(searchStudentReq);
        Page page = result.getData();
        List<Student> studentList = page.getRecords();
        List<Integer> stuList = studentList.stream().map(Student::getId).collect(Collectors.toList());

        LambdaQueryWrapper<Option> optionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Option> optionList = new ArrayList<>();
        Map<Integer,Subject> subjectMap = new HashMap<>();
        if (stuList.size() != 0) {
            optionLambdaQueryWrapper.in(Option::getStuId,stuList);
            optionLambdaQueryWrapper.in(Option::getElectiveSubId,electiveSubIdList);
            optionList = optionMapper.selectList(optionLambdaQueryWrapper);
            List<Integer> subIdList = optionList.stream().map(Option::getSubId).collect(Collectors.toList());
            LambdaQueryWrapper<Subject> subjectLambdaQueryWrapper = new LambdaQueryWrapper<>();
            subjectLambdaQueryWrapper.in(Subject::getId,subIdList);
            List<Subject> subjectList =  subjectMapper.selectList(subjectLambdaQueryWrapper);
            subjectMap = subjectList.stream().collect(Collectors.toMap(Subject::getId, Function.identity(),(e1,e2) -> e1));
        }
        Map<String,Option> optionMap = new HashMap<>();
        for (int i = 0; i < optionList.size(); i++) {
            optionMap.put(optionList.get(i).getStuId() + "-" + optionList.get(i).getElectiveSubId(),optionList.get(i));
        }
        List<StudentWithElectiveSubRes> studentWithElectiveSubResList = new ArrayList<>();
        for (int i = 0; i < studentList.size(); i++) {
            StudentWithElectiveSubRes studentWithElectiveSubRes = new StudentWithElectiveSubRes();
            BeanUtils.copyProperties(studentList.get(i), studentWithElectiveSubRes);
            List<StudentWithElectiveSubRes.ElectiveSubWithOptionSub> electiveSubWithOptionSubs = new ArrayList<>();
            for (int j = 0; j < electiveSubList.size(); j++) {
                StudentWithElectiveSubRes.ElectiveSubWithOptionSub electiveSubWithOptionSub = new StudentWithElectiveSubRes.ElectiveSubWithOptionSub();
                BeanUtils.copyProperties(electiveSubList.get(j), electiveSubWithOptionSub);
                if(optionMap.get(studentWithElectiveSubRes.getId() + "-" + electiveSubWithOptionSub.getId()) != null){
                    electiveSubWithOptionSub.setSubId(optionMap.get(studentWithElectiveSubRes.getId() + "-" + electiveSubWithOptionSub.getId()).getSubId());
                }
                if(electiveSubWithOptionSub.getSubId() != -1 && subjectMap.get(electiveSubWithOptionSub.getSubId()) != null){
                    electiveSubWithOptionSub.setSubName(subjectMap.get(electiveSubWithOptionSub.getSubId()).getName());
                }
                electiveSubWithOptionSubs.add(electiveSubWithOptionSub);
            }
            studentWithElectiveSubRes.setElectiveSubList(electiveSubWithOptionSubs);
            studentWithElectiveSubResList.add(studentWithElectiveSubRes);
        }
        page.setRecords(studentWithElectiveSubResList);
        return Result.success(page);
    }
}
